package com.wink_172.library.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.core.app.ActivityCompat;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.FragmentActivity;

import com.hjq.language.MultiLanguages;
import com.wink_172.library.AppManager;
import com.wink_172.library.R;

import org.xutils.common.Callback;

import java.util.ArrayList;


public class SmartActivity extends FragmentActivity implements View.OnClickListener, Callback.CommonCallback<String> {
    protected ProgressDialog progressDialog;
    public ICallback mCallback;

    @Override
    public void onClick(View v) {
    }


    public interface ICallback {
        void onSendEvent(int event, String content);
    }
    @Override
    protected void attachBaseContext(Context newBase) {
        // 国际化适配（绑定语种）
        super.attachBaseContext(MultiLanguages.attach(newBase));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_smart);

        if (Build.VERSION.SDK_INT >= 21) {
//            windowTranslucentStatus这个属性会和setStatusBarColor、键盘顶起等属性冲突,后期请注意这个地方
//            getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        }

//        setStatusBarLightMode();
        AppManager.getAppManager().addActivity(this);
        mHandler = new MyHandler();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    private static final String TAG = "TAG";

    //====>>xutils start
    @Override
    public void onSuccess(String result) {
        Log.e(TAG, "onSuccess: ====>>" + result.toString());
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        Log.e(TAG, "onError: ====>>"+ex.toString() );
    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {
        Log.e(TAG, "onFinished: ====>>");
            hideProgressDialog();
    }
    //<<====xutils end

    protected void onPermissionsGranted(int requestCode, boolean granted) {
    }


    public void requestPermissions(int requestCode, String[] permissions) {
        if (permissions != null && permissions.length > 0) {
            ActivityCompat.requestPermissions(this, permissions, requestCode);
        }
    }

    public boolean checkPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }

        ArrayList<String> remains = new ArrayList<String>();
        if (permissions != null) {
            for (String permission : permissions) {
                if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
                    continue;
                }
                remains.add(permission);
            }
        }

        return (remains.size() == 0);
    }

    public void setStatusBarLightMode(Object... args) {

        int color = getResources().getColor(R.color.white);
        if(args!=null&&args.length>0){
            color=(Integer)args[0];//自定义颜色
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //判断是否为小米或魅族手机，如果是则将状态栏文字改为黑色
//            if (MIUISetStatusBarLightMode(activity, true) || FlymeSetStatusBarLightMode(activity, true)) {
//                //设置状态栏为指定颜色
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0
//                    activity.getWindow().setStatusBarColor(color);
//                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4
//                    //调用修改状态栏颜色的方法
//                    setStatusBarColor(activity, color);
//                }
//            } else
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //如果是6.0以上将状态栏文字改为黑色，并设置状态栏颜色
                getWindow().setBackgroundDrawableResource(android.R.color.white);
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                getWindow().setStatusBarColor(color);

                //fitsSystemWindow 为 false, 不预留系统栏位置.
                ViewGroup mContentView = (ViewGroup) getWindow().findViewById(Window.ID_ANDROID_CONTENT);
                View mChildView = mContentView.getChildAt(0);
                if (mChildView != null) {
                    ViewCompat.setFitsSystemWindows(mChildView, true);
                    ViewCompat.requestApplyInsets(mChildView);
                }
            }
        }
    }
    public void showProgressDialog( Object... args) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this, R.style.CustomProgressDialog); progressDialog.show();
            progressDialog.setContentView(R.layout.dialog_progress);


            boolean touch=false;
            if(args!=null&&args.length>0){
                touch= (boolean) args[0];
            }
            progressDialog.setCancelable(touch);
            progressDialog.setCanceledOnTouchOutside(touch);
            progressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

        }else { progressDialog.show();}

    }

    public void hideProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
    public MyHandler getHandler() {
        return mHandler;
    }

    public MyHandler mHandler;

    public class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            handlerCallBack(msg);
        }
    }

    protected void handlerCallBack(Message msg) {

    }

}
